#define _CRT_SECURE_NO_WARNINGS  1

#include"Queue.h"

void QueueInit(Queue* pq)
{
	pq->head = pq->tail = NULL;
	pq->size = 0;
}

void QueueDestroy(Queue* pq)
{
	Qnode* cur = pq->head;
	while (cur)
	{
		Qnode* Next = cur->next;
		free(cur);
		cur = Next;
	}

	pq->head = pq->tail = NULL;
	pq->size = 0;
}

void QueuePush(Queue* pq, QDatatype x)
{
	Qnode* newnode = (Qnode*)malloc(sizeof(Qnode));
	newnode->val = x;
	if (pq->head ==  NULL)
	{
		pq->tail = pq->head = newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}

	newnode->next = NULL;
	pq->size++;
}

void QueuePop(Queue* pq)
{
	if (pq->head != NULL && pq->head == pq->tail)
		free(pq->head);
	else
	{
		Qnode* tmp = pq->head;
		pq->head = pq->head->next;
		free(tmp);

	}
	pq->size--;
}

int QueueSize(Queue* pq)
{
	return pq->size;
}

bool QueueEmpty(Queue* pq)
{
	return pq->size == 0;
}

QDatatype QueueFront(Queue* pq)
{
	return pq->head->val;
}

QDatatype QueueBack(Queue* pq)
{
	return pq->tail->val;
}